export const getCount = count => {
  if (count < 0) return
  else if (count < 10000) {
    return count
  } else if (count < Math.floor(count / 10000) < 10000) {
    return Math.floor(count / 1000) / 10 + '万'
  } else {
    return Math.floor(count / 10000000) / 10 + '亿'
  }
}

// 防抖
export const debounce = (func, delay) => {
  let timer
  return function (...args) {
    if (timer) {
      clearTimeout(timer)
    }

    timer = setTimeout(() => {
      func.apply(this, args)
      clearTimeout(timer)
    }, delay)
  }
}

// 找出第一个没有歌名的排行榜索引,该排行榜是全球榜
export const filterIndex = rankList => {
  for (let i = 0; i < rankList.length; i++) {
    if (rankList[i].tracks.length && !rankList[i + 1].tracks.length) {
      return i + 1
    }
  }
}

export const getName = list => {
  let str = ''

  list.map((item, index) => {
    str += index === 0 ? item.name : '/' + item.name
    return item
  })
  return str
}

export const isEmptyObj = obj => !obj || Object.keys(obj).length === 0

const elementStyle = document.createElement('div').style
const vendor = (() => {
  // 通过transition判断浏览器类型
  const transformNames = {
    webkit: 'webkitTransform',
    Moz: 'MozTransform',
    O: 'OTransform',
    ms: 'msTransform',
    standard: 'Transform'
  }

  for (let key in transformNames) {
    if (elementStyle[transformNames[key]] !== undefined) {
      return key
    }
  }

  return false
})()

export function prefixStyle(style) {
  if (vendor === false) {
    return false
  } else if (vendor === 'standard') {
    return style
  }

  // 首字母改成大写
  return vendor + style.charAt(0).toUpperCase() + style.substr(1)
}

export const getSongUrl = id => {
  return `https://music.163.com/song/media/outer/url?id=${id}.mp3`
}

export const formatPlayTime = interval => {
  interval = interval | 0
  const minute = (interval / 60) | 0
  const second = (interval % 60).toString().padStart(2, '0')

  return `${minute}:${second}`
}

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min)
}
// 随机打乱数组
export function shuffle(arr) {
  let newArr = []

  arr.forEach(item => {
    newArr.push(item)
  })

  for (let i = 0; i < newArr.length; i++) {
    let j = getRandomInt(0, i)
    let t = newArr(i)
    newArr[i] = newArr[j]
    newArr[j] = t
  }

  return newArr
}

export const findIndex = (song, list) => {
  return list.findIndex(item => {
    return song.id === item.id
  })
}
